package com.desmart.renjiao.main.home;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.desmart.renjiao.R;
import com.desmart.renjiao.bean.LoginBean;
import com.desmart.renjiao.main.adapter.HomeTypeAdapter;
import com.desmart.renjiao.main.adapter.HomeItemType;
import com.desmart.renjiao.main.bean.BannerView;
import com.desmart.renjiao.main.bean.EmptyLine;
import com.desmart.renjiao.main.bean.HomeIcons;
import com.desmart.renjiao.main.bean.Title;
import com.google.gson.Gson;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.common.CommonFragment;
import com.lemon.library.constants.PreferencesConstants;
import com.lemon.library.utils.PreferencesUtils;
import com.lemon.library.utils.StatusBarUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import java.util.ArrayList;
import java.util.List;

@Route(path = ARoutePath.Main.HomeFragment)
public class HomeFragment extends CommonFragment<HomeM,HomeP> implements Home.HomeV {

    private static ArrayList<HomeIcons.Icon> usedFunlist = new ArrayList<>();

    private RecyclerView recyclerview;
    private HomeTypeAdapter mAdapter;
    private RelativeLayout rlTitle;
    private static HomeIcons.Icon emailIcon;

    static {
        usedFunlist.add(new HomeIcons.Icon(R.string.the_news,R.drawable.icon_home_news));
        usedFunlist.add(new HomeIcons.Icon(R.string.jituangonggao,R.drawable.icon_hom_jituangonggao));
        usedFunlist.add(new HomeIcons.Icon(R.string.bendanweigonggao,R.drawable.icon_home_danweigonggao));
        usedFunlist.add(new HomeIcons.Icon(R.string.jianxun,R.drawable.icon_home_jianxun));
        usedFunlist.add(new HomeIcons.Icon(R.string.daiban,R.drawable.icon_home_daiban));
        emailIcon = new HomeIcons.Icon(R.string.youjian, R.drawable.icon_home_youjian);
        usedFunlist.add(emailIcon);
        usedFunlist.add(new HomeIcons.Icon(R.string.message,R.drawable.icon_home_message));
        usedFunlist.add(new HomeIcons.Icon(R.string.more,R.drawable.icon_home_more));
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_main_home;
    }

    @Override
    public void initView(View view) {

        SmartRefreshLayout refreshLayout = view.findViewById(R.id.refreshlayout);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setDragRate(0.3f);
        refreshLayout.setEnableOverScrollBounce(true);
        refreshLayout.setEnableOverScrollDrag(true);

        recyclerview = view.findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new HomeTypeAdapter(getPlaceHolderData(),4);
        recyclerview.setAdapter(mAdapter);

        rlTitle = view.findViewById(R.id.common_title_rl);
        setTopView();
    }

    private void setTopView() {
        int statusBarHeight = StatusBarUtil.getStatusBarHeight(getActivity());
        rlTitle.setPadding(0,statusBarHeight,0,0);
    }

    @Override
    public void initData() {
        String loginInfoStr = PreferencesUtils.getString(PreferencesConstants.login_info);
        if (loginInfoStr != null){
            LoginBean loginBean = new Gson().fromJson(loginInfoStr, LoginBean.class);
            if (loginBean != null)
                mPresenter.getEmail(loginBean.getName());
        }
    }

    public List<HomeItemType> getPlaceHolderData() {
        List<HomeItemType> list = new ArrayList<>();
        list.add(new BannerView());
        list.add(new EmptyLine());
        list.add(new HomeIcons(usedFunlist));
        list.add(new EmptyLine());
        return list;
    }

    @Override
    public void onGetEmailUnRead(String result) {
        emailIcon.setUnRead(result);
        mAdapter.notifyDataSetChanged();
    }
}
